package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.entity.dto.OrderDto;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired(required = false)
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order, HttpSession session){
        Long userId = (Long) session.getAttribute("user");

        order.setUserId(userId);

        orderService.submit(order);

        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page<OrderDto>> findByPage(Integer page,Integer pageSize,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        R<Page<OrderDto>> result = orderService.findByPage(page,pageSize,userId);
        return result;
    }

    @GetMapping("/page")
    public R<Page<Orders>> findAllOrder(Integer page, Integer pageSize, Long number,
                                        @RequestParam(defaultValue = "1900-01-01 00:00:00") String beginTime,
                                        @RequestParam(defaultValue = "2222-12-31 23:59:59") String endTime){

        R<Page<Orders>> result = orderService.findByNumberAndTime(page,pageSize,number,beginTime,endTime);
        return result;
    }
}
